﻿using System;
using Mono.Cecil.Cil;
using Mono.Cecil.PE;

namespace Mono.Cecil
{
	// Token: 0x0200001D RID: 29
	internal abstract class ModuleReader
	{
		// Token: 0x0600013C RID: 316 RVA: 0x00002E0A File Offset: 0x0000100A
		protected ModuleReader(Image image, ReadingMode mode)
		{
			this.module = new ModuleDefinition(image);
			this.module.ReadingMode = mode;
		}

		// Token: 0x0600013D RID: 317
		protected abstract void ReadModule();

		// Token: 0x0600013E RID: 318
		public abstract void ReadSymbols(ModuleDefinition module);

		// Token: 0x0600013F RID: 319 RVA: 0x00002E2C File Offset: 0x0000102C
		protected void ReadModuleManifest(MetadataReader reader)
		{
			reader.Populate(this.module);
			this.ReadAssembly(reader);
		}

		// Token: 0x06000140 RID: 320 RVA: 0x0000D894 File Offset: 0x0000BA94
		private void ReadAssembly(MetadataReader reader)
		{
			AssemblyNameDefinition assemblyNameDefinition = reader.ReadAssemblyNameDefinition();
			bool flag = assemblyNameDefinition == null;
			if (flag)
			{
				this.module.kind = ModuleKind.NetModule;
			}
			else
			{
				AssemblyDefinition assemblyDefinition = new AssemblyDefinition();
				assemblyDefinition.Name = assemblyNameDefinition;
				this.module.assembly = assemblyDefinition;
				assemblyDefinition.main_module = this.module;
			}
		}

		// Token: 0x06000141 RID: 321 RVA: 0x0000D8E8 File Offset: 0x0000BAE8
		public static ModuleDefinition CreateModule(Image image, ReaderParameters parameters)
		{
			ModuleReader moduleReader = ModuleReader.CreateModuleReader(image, parameters.ReadingMode);
			ModuleDefinition moduleDefinition = moduleReader.module;
			bool flag = parameters.assembly_resolver != null;
			if (flag)
			{
				moduleDefinition.assembly_resolver = Disposable.NotOwned<IAssemblyResolver>(parameters.assembly_resolver);
			}
			bool flag2 = parameters.metadata_resolver != null;
			if (flag2)
			{
				moduleDefinition.metadata_resolver = parameters.metadata_resolver;
			}
			bool flag3 = parameters.metadata_importer_provider != null;
			if (flag3)
			{
				moduleDefinition.metadata_importer = parameters.metadata_importer_provider.GetMetadataImporter(moduleDefinition);
			}
			bool flag4 = parameters.reflection_importer_provider != null;
			if (flag4)
			{
				moduleDefinition.reflection_importer = parameters.reflection_importer_provider.GetReflectionImporter(moduleDefinition);
			}
			ModuleReader.GetMetadataKind(moduleDefinition, parameters);
			moduleReader.ReadModule();
			ModuleReader.ReadSymbols(moduleDefinition, parameters);
			moduleReader.ReadSymbols(moduleDefinition);
			bool flag5 = parameters.ReadingMode == ReadingMode.Immediate;
			if (flag5)
			{
				moduleDefinition.MetadataSystem.Clear();
			}
			return moduleDefinition;
		}

		// Token: 0x06000142 RID: 322 RVA: 0x0000D9C4 File Offset: 0x0000BBC4
		private static void ReadSymbols(ModuleDefinition module, ReaderParameters parameters)
		{
			ISymbolReaderProvider symbolReaderProvider = parameters.SymbolReaderProvider;
			bool flag = symbolReaderProvider == null && parameters.ReadSymbols;
			if (flag)
			{
				symbolReaderProvider = new DefaultSymbolReaderProvider();
			}
			bool flag2 = symbolReaderProvider != null;
			if (flag2)
			{
				module.SymbolReaderProvider = symbolReaderProvider;
				ISymbolReader symbolReader = (parameters.SymbolStream != null) ? symbolReaderProvider.GetSymbolReader(module, parameters.SymbolStream) : symbolReaderProvider.GetSymbolReader(module, module.FileName);
				bool flag3 = symbolReader != null;
				if (flag3)
				{
					try
					{
						module.ReadSymbols(symbolReader, parameters.ThrowIfSymbolsAreNotMatching);
					}
					catch (Exception)
					{
						symbolReader.Dispose();
						throw;
					}
				}
			}
			bool flag4 = module.Image.HasDebugTables();
			if (flag4)
			{
				module.ReadSymbols(new PortablePdbReader(module.Image, module));
			}
		}

		// Token: 0x06000143 RID: 323 RVA: 0x0000DA84 File Offset: 0x0000BC84
		private static void GetMetadataKind(ModuleDefinition module, ReaderParameters parameters)
		{
			bool flag = !parameters.ApplyWindowsRuntimeProjections;
			if (flag)
			{
				module.MetadataKind = MetadataKind.Ecma335;
			}
			else
			{
				string runtimeVersion = module.RuntimeVersion;
				bool flag2 = !runtimeVersion.Contains("WindowsRuntime");
				if (flag2)
				{
					module.MetadataKind = MetadataKind.Ecma335;
				}
				else
				{
					bool flag3 = runtimeVersion.Contains("CLR");
					if (flag3)
					{
						module.MetadataKind = MetadataKind.ManagedWindowsMetadata;
					}
					else
					{
						module.MetadataKind = MetadataKind.WindowsMetadata;
					}
				}
			}
		}

		// Token: 0x06000144 RID: 324 RVA: 0x0000DAF0 File Offset: 0x0000BCF0
		private static ModuleReader CreateModuleReader(Image image, ReadingMode mode)
		{
			ModuleReader result;
			if (mode != ReadingMode.Immediate)
			{
				if (mode != ReadingMode.Deferred)
				{
					throw new ArgumentException();
				}
				result = new DeferredModuleReader(image);
			}
			else
			{
				result = new ImmediateModuleReader(image);
			}
			return result;
		}

		// Token: 0x04000082 RID: 130
		protected readonly ModuleDefinition module;
	}
}
